<?php
namespace app\api\controller;

use app\common\model as M;

/**
 * 实名认证模块
 */
class Idcard extends \app\api\ApiAuth
{
    public $model = '\app\common\model\user\UserIdcard';
	
    /**
     * 商城兑换订单
     * @Author   FW
     * @DateTime 2024-03-19
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
	public function info()
	{
        $info = $this->model::alias('idc')->field("ifnull(idc.realname,'') as realname,ifnull(idc.idnumber,'') as idnumber,ifnull(idc.address,'') as address,ifnull(idc.check_time,'') as check_time")
        ->join('__USER__ u' , 'idc.uid = u.uid' , 'RIGHT')
        ->where('u.uid',$this->uid)
        ->find();
		return $this->returnSuccess($info);
	}

    /**
     * 线路预定订单
     * @Author   FW
     * @DateTime 2024-03-19
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function saveInfo()
    {
        $info = $this->request->post();
        if ( !empty($info) ) {
            $this->model::startTrans();
            try {
                $info['uid'] = $this->uid;
                $info['check_time'] = date('Y-m-d H:i:s');
                $res = $this->model::saveOne($info);
                if ( $res === false ) throw new \Exception('保存失败【10001】');

                ## 更新用户实名及生日
                $birthday = idnumber2birthday($info['idnumber']);
                $res = M\User::where('uid',$this->uid)->update(['is_idcheck' => 1,'full_name' => $info['realname'], 'birthday' => $birthday ]);
                if ( $res === false ) throw new \Exception('保存失败【10002】');

                $this->model::commitTrans();
                return $this->returnSuccess();
            } catch ( \Exception $e ) {
                $this->model::rollbackTrans();
                return $this->returnFail($e->getMessage());
            }
        }

        $this->model::rollbackTrans();
        return $this->returnFail('保存失败');
    }
}//Class End